package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.*;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = {"订单接口"})
public class OrderController {
    @Autowired
    OrderService orderService;

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> order(@RequestBody OrdersSubmitDTO dto){
        log.info("用户下单{}",dto);
        OrderSubmitVO orderVO = orderService.addOrder(dto);
        return Result.success(orderVO);
    }

    @GetMapping("/historyOrders")
    @ApiOperation("用户查询历史订单")
    public Result<PageResult> queryHistoryOrders(OrdersPageQueryDTO dto){
        log.info("用户查询历史订单");
        PageResult orders = orderService.queryHistoryOrders(dto);
        return Result.success(orders);
    }

    @GetMapping("/orderDetail/{id}")
    public Result<OrderHistoryVO> queryOrderDetail(@PathVariable Long id){
        log.info("用户查询id为:{}订单详细信息",id);
        OrderHistoryVO orderHistoryVO = orderService.queryOrderDetailByOrderId(id);
        return Result.success(orderHistoryVO);
    }

    @PostMapping("/repetition/{orderId}")
    public Result addRepetitionOrder(@PathVariable Long orderId){
        log.info("用户再来一单，原订单id：{}",orderId);
        orderService.repeatOrder(orderId);
        return Result.success();
    }

    @PutMapping("/cancel/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
        log.info("取消id为{}的订单",orderId);
        orderService.cancelOrder(orderId);
        return Result.success();
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @GetMapping("/reminder/{orderId}")
    public Result reminder(@PathVariable Long orderId){
        orderService.reminder(orderId);
        return Result.success();
    }
}
